package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IBookingsDatetimeDAO;
import org.jsola.hr.dto.BookingsDatetimeAddDTO;
import org.jsola.hr.dto.BookingsDatetimeUpdateDTO;
import org.jsola.hr.entity.BookingsDatetimeDO;
import org.jsola.hr.query.BookingsDatetimeQuery;
import org.jsola.hr.service.IBookingsDatetimeService;
import org.jsola.hr.vo.BookingsDatetimeListVO;
import org.jsola.hr.vo.BookingsDatetimeVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 预约业务时段表
 *
 * @author zhr
 */
@Service("hrBookingsDatetimeServiceImpl")
public class BookingsDatetimeServiceImpl implements IBookingsDatetimeService {

    @Autowired
    private IBookingsDatetimeDAO bookingsDatetimeDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public BookingsDatetimeVO save(BookingsDatetimeAddDTO bookingsDatetimeAddDTO, TokenUser tokenUser) {
        // 转DO
        BookingsDatetimeDO bookingsDatetimeDO = bookingsDatetimeAddDTO.to(BookingsDatetimeDO.class);
        // 保存
        bookingsDatetimeDO = save(bookingsDatetimeDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return bookingsDatetimeDO.to(BookingsDatetimeVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(BookingsDatetimeUpdateDTO bookingsDatetimeUpdateDTO, TokenUser tokenUser) {
        //转DO
        BookingsDatetimeDO bookingsDatetimeDO = bookingsDatetimeUpdateDTO.to(BookingsDatetimeDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(bookingsDatetimeDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, Long... bookingsDatetimeIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) bookingsDatetimeIds);
    }


    @Override
    public BookingsDatetimeVO selectById(Long bookingsDatetimeId, String siteId) {
        BookingsDatetimeDO bookingsDatetimeDO = selectDOById(bookingsDatetimeId, siteId);
        if (bookingsDatetimeDO == null) {
            return null;
        }
        return bookingsDatetimeDO.to(BookingsDatetimeVO.class);
    }

    @Override
    public List<BookingsDatetimeListVO> select(BookingsDatetimeQuery bookingsDatetimeQuery, String siteId) {
        List<BookingsDatetimeDO> bookingsDatetimeDOList = selectDO(bookingsDatetimeQuery, siteId);
        if(CollectionUtils.isEmpty(bookingsDatetimeDOList)) {
            return bookingsDatetimeDOList == null ? null : new ArrayList<>();
        }
        return bookingsDatetimeDOList.stream()
                .map(bookingsDatetimeDO -> bookingsDatetimeDO.to(BookingsDatetimeListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(BookingsDatetimeQuery bookingsDatetimeQuery, String siteId) {
        Example example = buildExample(bookingsDatetimeQuery,siteId);
        return bookingsDatetimeDAO.selectCountByExample(example);
    }

    @Override
    public Page<BookingsDatetimeListVO> selectPage(BookingsDatetimeQuery bookingsDatetimeQuery, String siteId) {
        Example example = buildExample(bookingsDatetimeQuery,siteId);
        Page<BookingsDatetimeDO> page = bookingsDatetimeDAO.selectPageByExample(example,
                bookingsDatetimeQuery.getPageNo(),
                bookingsDatetimeQuery.getPageSize());

        return page.to(BookingsDatetimeListVO.class);
    }

    @Override
    public List<BookingsDatetimeDO> selectByBookingsBusinessId(Long bookingsBusinessId) {
        Example example = new Example(BookingsDatetimeDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("businessId", bookingsBusinessId);
        return bookingsDatetimeDAO.selectByExample(example);
    }

    @Override
    public BookingsDatetimeDO selectDOById(Long bookingsDatetimeId, String siteId) {
        return listById(bookingsDatetimeId, siteId);
    }

    @Override
    public List<BookingsDatetimeDO> selectDO(BookingsDatetimeQuery bookingsDatetimeQuery, String siteId) {
        Example example = buildExample(bookingsDatetimeQuery,siteId);
        return bookingsDatetimeDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param bookingsDatetimeQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(BookingsDatetimeQuery bookingsDatetimeQuery, String siteId) {
        Example example = new Example(BookingsDatetimeDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example,bookingsDatetimeQuery.getOrders());
        return example;
    }
}




